<?php

class Taxi_Model_Solicituds extends Zend_Db_Table_Abstract
{
    protected $_name = 'solicituds';
    protected $_primary = 'id_solicitud';
    
    public function consultar_solicitud_perID($id_solicitud)
    {
        $where = $this->_db->select()
                    ->from(array ('s'=>$this->_name),
                            array ('posX','posY','dia','hora'))
                    ->where('id_solicitud = ?', $id_solicitud);
        
        $result = $where->query()->fetchAll();
        
        if (sizeof($result)== 0)
        {
            return -1;
        }else
        {
            return $result;
        }
    }
    
    public function consultar_solicitud_perMAIL($mail)
    {
        $dia = $this->_db->select()
                    ->from($this->_name,'dia')
                    ->where('mail_taxi = ?', $mail);
        
        $hora = $this->_db->select()
                    ->from($this->_name,'posY')
                    ->where('mail_taxi = ?', $mail);
        
        $resDia = $dia->__toString();
        $resHora = $hora->__toString();
        
        if ($resDia != NULL && $resHora != NULL) {
            $where = $this->_db->select()
                    ->from(array ('s'=>$this->_name),
                            array ('id_solicitud','posX','posY','dia','hora'))
                    ->where('mail_taxi = ?', $mail);
        
            $result = $where->query()->fetchAll();
        
            if (sizeof($result)== 0)
            {
                return -4;
            }else
            {
                return $result;
            }
        }else {
            return -3;
        }
    }

    public function consultar_positions($id_solicitud)
    {
        $posX = $this->_db->select()
                    ->from($this->_name,'posX')
                    ->where('id_solicitud = ?', $id_solicitud);
        
        $posY = $this->_db->select()
                    ->from($this->_name,'posY')
                    ->where('id_solicitud = ?', $id_solicitud);
        
        
        $resultX = $posX->__toString();
        $resultY = $posY->__toString();
        
        $result= array($resultX,$resultY);
        
        if (sizeof($result)== 0)
        {
            return -1;
        }else
        {
            return $result;
        }
    }
    
    public function consulta_dia($id_solicitud)
    {
        $dia = $this->_db->select()
                    ->from($this->_name,'dia')
                    ->where('id_solicitud = ?', $id_solicitud);
        $result = $dia->__toString();
        
        if ($result == NULL)
        {
            return 0;
        }else
        {
            return 1;
        }
    }
    
    public function cancelar_solicitud($id_solicitud){
        
        $where = $this->getAdapter()->quoteInto('id_solicitud = ?', $id_solicitud);
        
        $res = $this->delete($where);
        
        if ($res == 0)
        {
            return -5;
        }else
        {
            return 0;
        }
    }
    
    public function accept_solicitud($id_solicitud,$mail)
    {
        $data = array('mail_taxi' =>  $mail);
        $where = $this->getAdapter()->quoteInto('id_solicitud = ?', $id_solicitud);
        
        $update = $this->update($data, $where);
        
        if ($update == 0)
        {
            return -1;
        }else
        {
            return 0;
        } 
        
    }
    
    public function reject_solicitud($id_solicitud)
    {
        $data = array( 'mail_taxi' =>  '0');
        $where = $this->getAdapter()->quoteInto('id_solicitud = ?', $id_solicitud);
        
        $update = $this->update($data, $where);
        
        if ($update == 0)
        {
            return -3;
        }else
        {
            return 0;
        } 
    }
}

